package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SupplierInvoiceServlet
 */
@WebServlet("/SupplierInvoiceServlet")
public class SupplierInvoiceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SupplierInvoiceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
			PrintWriter out = response.getWriter();
			String dt1="",nm1="",cd1="",in1="",amt1="",amtadj1="",balamt1="",cr1="";
			
			String dt[]=request.getParameterValues("dte");
			String nm[]=request.getParameterValues("sname");
			String cd[]=request.getParameterValues("scd");
			String in[]=request.getParameterValues("inn");
			 
			String amt[]=request.getParameterValues("innamt");
		String amtadj[]=request.getParameterValues("amtadjust");
			String balamt[]=request.getParameterValues("balamt");
			String cr[]=request.getParameterValues("cred");
			int j=0;
			 Class.forName("com.mysql.jdbc.Driver");
			
		        Connection   con = DriverManager.getConnection("jdbc:mysql://localhost/boa", "root", "root");
		        Statement st=null;
		        String  query="";
		        int[] iNoRows=null;
		        st=con.createStatement();
	        /// PreparedStatement ps=con.prepareStatement(query);
	        // ps.setString(1, country);
		        for (int i=0;i<in.length;i++) {
		        	
			        cd1=cd[i];
			      String  query1="delete from purchaseinvoiceoutstanding where scode = '"+cd1+"';";
			      st.executeUpdate(query1);
			        }
		         for (int i=0;i<in.length;i++) {
		        	 dt1=dt[i];
		        	 nm1=nm[i];
		        	 cd1=cd[i];
		        	 cr1=cr[i];
		        	 in1=in[i];
		        	 amt1=amt[i];
		        	 amtadj1=amtadj[i];
		        	 balamt1=balamt[i];
		        	 
		        	 query="INSERT INTO purchaseinvoiceoutstanding(date,pvinno,pvname,scode,pvamount,adjamount,balance,pvcred) VALUES('"+dt1+"','"+in1+"','"+nm1+"','"+cd1+"','"+amt1+"','"+amtadj1+"','"+balamt1+"','"+cr1+"')";
		        	 j=st.executeUpdate(query);
		        	 
		        	}
		         
		        	con.close();
		        	
	     }catch(Exception e){
	         e.printStackTrace();
	     }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
